package com.xiebishe.mylibrary.common.pageStatus;

import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.viewpager.widget.ViewPager;

import com.xiebishe.mylibrary.common.pageStatus.centerHandler.AddRemoveInParentStatusHandler;
import com.xiebishe.mylibrary.common.pageStatus.centerHandler.ConstraintLayoutHandler;
import com.xiebishe.mylibrary.common.pageStatus.centerHandler.FrameLayoutHandler;
import com.xiebishe.mylibrary.common.pageStatus.centerHandler.LinearLayoutHandler;
import com.xiebishe.mylibrary.common.pageStatus.centerHandler.RelativeLayoutHandler;
import com.xiebishe.mylibrary.common.pageStatus.topHandler.AddRemoveInParentTopStatusHandler;
import com.xiebishe.mylibrary.common.pageStatus.topHandler.ConstraintLayoutTopHandler;
import com.xiebishe.mylibrary.common.pageStatus.topHandler.FrameLayoutTopHandler;
import com.xiebishe.mylibrary.common.pageStatus.topHandler.LinearLayoutTopHandler;
import com.xiebishe.mylibrary.common.pageStatus.topHandler.RelativeLayoutTopHandler;


/**
 * 这个是默认的状态机的实现类，内部通过组合支持多种layout的状态替换
 *
 * @author wangwenbo
 */
public class DefaultStatusHandler implements StatusHandler {

    private AbstractStatusHandler delegate;

    public DefaultStatusHandler(@NonNull ViewGroup layout, View.OnClickListener retry) {
        init(layout);
        ErrorView errorView = new ErrorView(layout.getContext());
        LoadingView loadingView = new LoadingView(layout.getContext());
        EmptyView emptyView = new EmptyView(layout.getContext());
        errorView.setLoadingListener(retry);
        setErrorView(errorView);
        setLoadingView(loadingView);
        setEmptyView(emptyView);
    }

    public DefaultStatusHandler(int inTopCenter, @NonNull ViewGroup layout, View.OnClickListener retry) {
        init(inTopCenter, layout);
        ErrorView errorView = new ErrorView(layout.getContext());
        LoadingView loadingView = new LoadingView(layout.getContext());
        EmptyView emptyView = new EmptyView(layout.getContext());
//        emptyView.setLoadingListener(retry);
        errorView.setLoadingListener(retry);
        setErrorView(errorView);
        setLoadingView(loadingView);
        setEmptyView(emptyView);
    }

    public DefaultStatusHandler(@NonNull ViewGroup layout) {
        init(layout);
    }

    public DefaultStatusHandler(int inTopCenter, @NonNull ViewGroup layout) {
        init(inTopCenter, layout);
    }


    private void init(ViewGroup layout) {
        if (layout instanceof LinearLayout) {
            this.delegate = new LinearLayoutHandler((LinearLayout) layout);
        } else if (layout instanceof FrameLayout) {
            this.delegate = new FrameLayoutHandler((FrameLayout) layout);
        } else if (layout instanceof ConstraintLayout) {
            this.delegate = new ConstraintLayoutHandler((ConstraintLayout) layout);
        } else if (layout instanceof RelativeLayout) {
            this.delegate = new RelativeLayoutHandler((RelativeLayout) layout);
        } else {
            this.delegate = new AddRemoveInParentStatusHandler<>(layout);
        }
    }

    private void init(int inTopCenter, ViewGroup layout) {
        if (layout instanceof LinearLayout) {
            this.delegate = new LinearLayoutTopHandler((LinearLayout) layout, inTopCenter);
        } else if (layout instanceof FrameLayout) {
            this.delegate = new FrameLayoutTopHandler((FrameLayout) layout, inTopCenter);
        } else if (layout instanceof ConstraintLayout) {
            this.delegate = new ConstraintLayoutTopHandler((ConstraintLayout) layout, inTopCenter);
        } else if (layout instanceof RelativeLayout) {
            this.delegate = new RelativeLayoutTopHandler((RelativeLayout) layout, inTopCenter);
        } else {
            this.delegate = new AddRemoveInParentTopStatusHandler<>(layout, inTopCenter);
        }
    }

    /**
     * 返回这个layout 是否被完全支持。{@link AddRemoveInParentStatusHandler}这种只是取巧，在layout的parent为{@link ViewPager ViewPager}等有adapter的veiw，或者coordinatorlayout时，会有问题。
     *
     * @param layout 是否完全支持的layout
     * @return true 完全支持
     */
    public static boolean isCompleteSuppert(@NonNull ViewGroup layout) {
        return layout instanceof LinearLayout
                || layout instanceof FrameLayout
                || layout instanceof RelativeLayout
                || layout instanceof ConstraintLayout;
    }

    public View getLoadingView() {
        return delegate.getLoadingView();
    }

    public void setLoadingView(View loadingView) {
        delegate.setLoadingView(loadingView);
    }

    public View getErrorView() {
        return delegate.getErrorView();
    }

    public void setErrorView(View errorView) {
        delegate.setErrorView(errorView);
    }

    public View getEmptyView() {
        return delegate.getEmptyView();
    }

    public void setEmptyView(View emptyView) {
        delegate.setEmptyView(emptyView);
    }

    @Override
    public void loading() {
        delegate.loading();
    }

    @Override
    public void showContent() {
        delegate.showContent();
    }

    @Override
    public void error() {
        delegate.error();
    }

    @Override
    public void empty() {
        delegate.empty();
    }

    @Override
    public void addOnStatusChangeListener(OnStatusChangeListener listener) {
        delegate.addOnStatusChangeListener(listener);
    }

    @Override
    public void removeOnStatusChangeListener(OnStatusChangeListener listener) {
        delegate.removeOnStatusChangeListener(listener);
    }

    @Override
    public void clearOnStatusChangeListener() {
        delegate.clearOnStatusChangeListener();
    }

    @Override
    public int getStatus() {
        return delegate.getStatus();
    }
}
